package csc.traning.wpsj.servlet;

import java.io.File;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.org.apache.bcel.internal.classfile.Field;

import csc.training.wpsj.validate.InputValidation;
import csc.traning.wpsj.dao.UserDAO;
import csc.traning.wpsj.domain.User;


/**
 * Servlet implementation class RegisterServlet
 */
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private static final String ERROR_TEXT ="is required </li>" ;
	
    public RegisterServlet() {
        super();
        // TODO Auto-generated constructor stub
    }


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	
		String error="";
		String page="";
		InputValidation validation = new InputValidation();
		String firstName=request.getParameter("firstName");
		String lastName=request.getParameter("lastName");
		String userName=request.getParameter("userName");
		String email= request.getParameter("email");
		String password= request.getParameter("password");
		String confirmPassword= request.getParameter("confirmPassword");
		String cellphone= request.getParameter("cellphone");
		
		request.setAttribute("firstName",firstName);
		request.setAttribute("lastName",lastName);
		request.setAttribute("userName",userName);
		request.setAttribute("email",email);
		request.setAttribute("cellphone",cellphone);
		if (validation.checkEmpty(firstName)) {
			error+="<li>First name "+ERROR_TEXT;
		}
		if (validation.checkEmpty(lastName)) {
			error+="<li>Last name "+ERROR_TEXT;
		}
		if (validation.checkEmpty(userName)) {
			error+="<li>Username "+ERROR_TEXT;
		}
		if (validation.checkEmpty(email)) {
			error+="<li>Email "+ERROR_TEXT;
		}
		if (validation.checkEmpty(password)) {
			error+="<li>Password "+ERROR_TEXT;
		}
		
		
		if (!validation.checkEmpty(email)&&validation.checkEmailFormat(email)==false) {
			error+="<li> Email is not valid format </li>";
		}
		if (validation.comparePassword(password,confirmPassword)==false) {
			error+="<li>Password field and confirm password field must be the same </li>";
		}
		
		if (!validation.checkEmpty(cellphone)&&validation.checkCellphone(cellphone)==false) {
			error+="<li>Cellphone must be number </li>";
		}
		
	
		if (!validation.checkEmpty(firstName) && !validation.checkEmpty(lastName) 
				&&!validation.checkEmpty(userName) && !validation.checkEmpty(email) 
				&& !validation.checkEmpty(password)
				&& validation.checkEmailFormat(email)==true 
				&& validation.comparePassword(password,confirmPassword)==true
				)
		{
			
			User user = new User(userName, password, firstName, lastName, cellphone, email, "1");
			UserDAO userDAO = new UserDAO();
			try {
				if (userDAO.addAccount(user)) {

					
				
					
					response.sendRedirect("./logon.jsp");
					System.out.println("alo");
				}
				else{
					error+="<li>Username is existed</li>";
					request.setAttribute("error", error);
					RequestDispatcher requestDispatcher = request.getRequestDispatcher("/profileRegister.jsp");
					requestDispatcher.forward(request, response);
				}
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
		}
		
		else{
			request.setAttribute("error", error);
			RequestDispatcher requestDispatcher = request.getRequestDispatcher("/profileRegister.jsp");
			requestDispatcher.forward(request, response);
			
			
		}
		
	}
	
	
}
